import Vue from "vue";
import VueRouter from "vue-router";
import { routes } from "./routes";
import { whiteList } from "@/router/whiteList";
import { getToken, getUserid } from "@/utils/auth";
import store from "../store/index";

Vue.use(VueRouter);

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject);
  return originalPush.call(this, location).catch((err) => err);
};
const router = new VueRouter({
  routes,
});

// router.beforeEach(async (to, from, next) => {
//   // debugger
//   // next()
//   // console.log('while to.path：' + to.path)
//   // 下面除非能改好，不然别改动，顺序也不要动
//   if (to.path =='/home/personInfo' || to.path == '/home/newDiscCode') {
//     const hasToken = getToken();
//     if (hasToken) {
//       // const userid = store.getters.userid;
//       const userid = getUserid();
//       if (userid) {
//         next();
//       }else{
//         next({ path: "/home/login" });
//       }
//     }else{
//       next({ path: "/home/login"});
//     }
//   }
//   else{
//     if (
//       whiteList.indexOf(to.path) !== -1 ||
//       to.path.indexOf("/redirect") !== -1
//     ) {
//       // 在免费登录白名单中，直接登录
//       //console.log('while to.path：' + to.path)
//       next({ replace: true });
//     }
//   }

// });
router.beforeEach(async (to, from, next) => {
  await store.dispatch("Routes/generateRoutes");
  // to.path === "/home/login"
  if ( to.path === "/home") {
    next('/briefHome');
  }
  if (to.path == "/briefHome" ) {
    // 做首页的播放隐藏和底部的播放显示
    store.dispatch("Routes/set_IsShowFooter", true);
  } else {
    store.dispatch("Routes/set_IsShowFooter", false);
  }
  next();
});
// router.afterEach()
export default router;
